package com.peanut.controller;

import com.peanut.entity.Userentity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;

/**
 * @className: com.peanut.controller-> HttpController
 * @description: 这个类是做做什么得吖，小朋友？
 * @author: chenhs
 * @createDate: 2021-08-15 21:31
 * @version: 1.0
 */
@Controller
public class HttpController {

    //    测试异常处理基于注解配置
    @RequestMapping(value = "/testExceptionNote")
    public String testExceptionNote() {
        int a=1/0;
        return "success";
    }

    //    测试异常处理基于xml配置
    @RequestMapping(value = "/testException")
    public String testException() {
        String srt = null;
        srt.toString();
        return "success";
    }
/**
 * @title testInterceptor
 * @description 测试拦截器
 * @author cehnhs
 * @updateTime 2021/9/7 20:54
 * @return
 */
    @RequestMapping(value = "/testInterceptor")
    public String testInterceptor() {
        return "success";
    }
    /**
     * @title
     * @description 文件上传
     * @author cehnhs
     * @param
     * @updateTime 2021/9/2 22:29
     * @return
     */
    @RequestMapping(value = "/testUp")
    public String testUp(MultipartFile mFile, HttpSession session) throws IOException {
        ServletContext context = session.getServletContext();
        String UPPath = context.getRealPath("UP");
        //获取文件名
        String fileName = mFile.getOriginalFilename();
      //用uuid替换原有的文件民称 目的是防止文件名称重复。
       if(fileName!=null){
           fileName =  UUID.randomUUID().toString()+fileName.substring(fileName.lastIndexOf("."));
       }
        File tempFile = new File(UPPath);
        if(!tempFile.exists()){
           // 文件目录不存在
            tempFile.mkdir();
        }
        //文件路径
        String finalPath = UPPath + File.separator + fileName;
        //上传
        mFile.transferTo(new File(finalPath));
        return "success";
    }
    @RequestMapping(value = "/testRequsetBody")
    public String testRequsetBody(@RequestBody String requsetbody) {
        System.out.println(requsetbody);
        return "success";
    }

    @RequestMapping(value = "/testRequsetEntity")
    public String testRequsetEntity(RequestEntity<String>  stringRequestEntity) {

        HttpHeaders headers = stringRequestEntity.getHeaders();
        System.out.println("stringRequestEntity!!!!"+stringRequestEntity.toString());
        System.out.println("getHeaders!!!!"+stringRequestEntity.toString());
        System.out.println("getHeaders!!!!"+headers);
        return "success";
    }

    @RequestMapping(value = "/testResponse")
    //  这里不能有返回值，有返回值表示这就是返回的一个视图，也就是返回的一个页面
    public void name(HttpServletResponse response) throws IOException {
        response.getWriter().println("你好 respsonse");
    }
    /**
     * @title
     * @description 响应浏览器数据 @ResponseBody 注解
     * 作用:标识这个方法返回的数据不是一个视图名称而是响应的数据。
     * 默认情况下响应的是一个页面，加了 @ResponseBody 注解响应的就是文字，不会跳转页面。
     * @author cehnhs
     * @param
     * @updateTime 2021/8/28 8:18
     * @return
     */
    @RequestMapping(value = "/testResponseBoby")
    @ResponseBody
    public String testResponseBoby() {
    //   没有 @ResponseBody 返回一个视图名称，加上后返回的返回的是文字
        return "@ResponseBody 响应数据。";
    }
    /**
     * @title
     * @description 测试响应实体
     *    需要这依赖，有了这个依赖，实体会自动转换为json响应的前台
     *     <dependency>
     *       <groupId>com.fasterxml.jackson.core</groupId>
     *       <artifactId>jackson-databind</artifactId>
     *       <version>2.12.1</version>
     *     </dependency>
     *
     *
     * @author cehnhs
     * @param
     * @updateTime 2021/8/28 8:57
     * @return
     */
    @RequestMapping(value = "/testEntity")
    @ResponseBody
    public Userentity testEntity() {
        return  new Userentity("张三","123");
    }

    @RequestMapping(value = "/testEntitys")
    @ResponseBody
    public ArrayList<Userentity> testEntitys() {
        ArrayList<Userentity> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new Userentity("张三","123"));
        }
        return list ;
    }

    @RequestMapping(value = "/testAjax")
    @ResponseBody
    public ArrayList<Userentity> testAjax(Userentity userentity) {
        System.out.println("'------------------------------------------------'");
        System.out.println(userentity.toString());
        System.out.println("'------------------------------'");

        ArrayList<Userentity> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new Userentity("张三","123"));
        }
        return list ;
    }

}
